package com.css.fxfzypg.modules.resultupload.service.impl;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.SettingSeismic.entity.SettingSeismicDetails;
import com.css.fxfzypg.modules.SettingSeismic.repository.SettingSeismicRepository;
import com.css.fxfzypg.modules.SettingSeismic.vo.PointVO;
import com.css.fxfzypg.modules.baseData.entity.PbeEntity;
import com.css.fxfzypg.modules.baseData.service.PbeService;
import com.css.fxfzypg.modules.baseData.vo.PbeEntityVO;
import com.css.fxfzypg.modules.earthquakedisasterloss.repository.SettingSeismicInfluencefliedRepository;
import com.css.fxfzypg.modules.earthquakedisasterloss.vo.SeismicInfluencefliedVO;
import com.css.fxfzypg.modules.maps.service.InfluencefliedMapService;
import com.css.fxfzypg.modules.resultupload.repository.ResultTotalRepository;
import com.css.fxfzypg.modules.resultupload.repository.entity.ResultDetailsEntity;
import com.css.fxfzypg.modules.resultupload.repository.entity.ResultMainEntity;
import com.css.fxfzypg.modules.resultupload.service.ResultTotalService;
import com.css.fxfzypg.modules.resultupload.vo.ResultMainEntityVO;
import com.css.fxfzypg.util.*;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: fxfzypg
 * @Author: lhl
 * @CreateDate: 2022/11/3 17:08
 */
@Service
public class ResultTotalServiceImpl implements ResultTotalService {
    @Resource
    SUserService sUserService;
    @Resource
    SettingSeismicInfluencefliedRepository settingSeismicInfluencefliedRepository;
    @Resource
    PbeService pbeService;
    @Resource
    ResultTotalRepository resultTotalRepository;

    @Resource
    InfluencefliedMapService influencefliedMapService;//地图使用
    @Resource
    SettingSeismicRepository settingSeismicRepository;
    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public void saveYpgResult(String taskId,String range) {
        /*resultTotalRepository.delResultTotalByTaskId(taskId, "2");
        resultTotalRepository.delResultDetailsEntityByTaskId(taskId, "2");*/
        //zyt修改
        resultTotalRepository.delResultTotalByTaskId(taskId, null);
        resultTotalRepository.delResultDetailsEntityByTaskId(taskId, null);

        String userId = PlatformSessionUtils.getUserId();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);

        Map<String, ResultMainEntity> listMap = new HashMap<>();
        List<PointVO> pointVOByTaskId = settingSeismicInfluencefliedRepository.getPointVOByTaskId(taskId);
        for (PointVO pointVO : pointVOByTaskId) {
            List<SeismicInfluencefliedVO> list1 = settingSeismicInfluencefliedRepository.findByPointVO(taskId,pointVO);

            for (SeismicInfluencefliedVO s : list1) {
                ResultDetailsEntity entity = new ResultDetailsEntity();
                entity.setId(UUIDGenerator.getUUID());
                entity.setTaskId(taskId);
                entity.setSort(String.valueOf(s.getFaultNum()));
                entity.setLongitude(String.valueOf(s.getLongitude()));
                entity.setLatitude(String.valueOf(s.getLatitude()));
                entity.setMacroPosition(s.getMacroPosition());
                entity.setMagnitude(s.getEarthquakeLevel());
                entity.setAngle(String.valueOf(s.getAngle()));
                //根据地震影响场id去获取当前公里格网 或者 根据地震影响场id和任务id 获取相交部分的公里格网 'pbe'
                List<PbeEntityVO> pbe = null;
                BigDecimal disasterArea2=BigDecimal.ZERO;
                if (range.equals("0")){
                    //根据地震影响场id去获取当前公里格网
                    pbe=pbeService.getPbe2ByInfluenceflieId(s.getInfluencefliedId());
                    disasterArea2 = BigDecimal.valueOf(Math.PI).multiply(s.getLongX().divide(BigDecimal.valueOf(2), 4, BigDecimal.ROUND_UP)
                            .multiply(s.getShortY().divide(BigDecimal.valueOf(2), 4,BigDecimal.ROUND_UP)));
                }else {
                    //根据地震影响场id和任务id 获取相交部分的公里格网 'pbe'
                    pbe=pbeService.getPbe2ByInfluenceflieIdAndTaskId(s.getInfluencefliedId(),taskId);
                    //影响场与任务范围相交求受灾面积 单位平方公里
                    Integer disasterArea = settingSeismicRepository.getArea(s.getInfluencefliedId(),taskId);
                    //disasterArea2=BigDecimal.valueOf(pbe.size());
                    disasterArea2 = BigDecimal.valueOf(disasterArea);
                }
                BigDecimal disasterNum2 = BigDecimal.ZERO;
                BigDecimal disasterGDP2 = BigDecimal.ZERO;
                disasterNum2 = disasterNum2.add(BigDecimal.valueOf(pbe.stream().mapToDouble(PbeEntityVO::getPopulation).sum()));
                disasterGDP2 = disasterGDP2.add(BigDecimal.valueOf(pbe.stream().mapToDouble(PbeEntityVO::getGdp).sum()));

                entity.setDisasterArea(disasterArea2.setScale(0, BigDecimal.ROUND_UP));
                disasterNum2=disasterNum2.divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_UP);
                entity.setDisasterNum(disasterNum2.toString());
                disasterGDP2=disasterGDP2.divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_UP);
                entity.setDisasterGdp(disasterGDP2.toString());
                entity.setDataSource("2");
                entity.setDelFlag(YNEnum.N.toString());
                entity.setCreateId(userId);
                entity.setCreateName(sUser.getUserName());
                entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                resultTotalRepository.saveResultDetailsEntity(entity);

                String key = taskId + "-" + pointVO.getFaultNum() + "-" + pointVO.getLongitude() + "-" + pointVO.getLatitude() + "-"
                        + pointVO.getMacroPosition() + "-" + pointVO.getEarthquakeLevel() + "-" + "2";
                ResultMainEntity resultMainEntity = null;
                if (listMap.containsKey(key)) {
                    resultMainEntity = listMap.get(key);
                    resultMainEntity.setDisasterArea(disasterArea2);
                    for (int i = 0; i < 2; i++) {
                        if (i == 0) {
                            String s1 = compareTo(disasterNum2.setScale(2, BigDecimal.ROUND_UP), resultMainEntity.getDisasterNum());
                            resultMainEntity.setDisasterNum(s1);
                        } else {
                            String s1 = compareTo(disasterGDP2.setScale(2, BigDecimal.ROUND_UP), resultMainEntity.getDisasterGdp());
                            resultMainEntity.setDisasterGdp(s1);
                        }
                    }
                } else {
                    resultMainEntity = new ResultMainEntity();
                    resultMainEntity.setCreateId(userId);
                    resultMainEntity.setCreateName(sUser.getUserName());
                    resultMainEntity.setDisasterArea(disasterArea2);
                    resultMainEntity.setDisasterNum(disasterNum2.setScale(2, BigDecimal.ROUND_UP).toString() + "-0");
                    resultMainEntity.setDisasterGdp(disasterGDP2.setScale(2, BigDecimal.ROUND_UP).toString() + "-0");
                    listMap.put(key, resultMainEntity);
                }
            }

        }
        saveResultMainEntity(listMap);
    }

    //zyt修改--导入影响场保存start
    public int delResultTotalByTaskIdNew(String taskId, SettingSeismicDetails settingSeismicDetails) {
        int i = -1;
        try {
            String sql = "delete from ypg_result_total " +
                    "where task_id='"+taskId+"' and sort='"+settingSeismicDetails.getFaultNum()+"' " +
                    "and magnitude='"+settingSeismicDetails.getEarthquakeLevel()+"'";

            i = jdbcTemplate.update(sql);

        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    public int delResultDetailsEntityByTaskIdNew(String taskId, SettingSeismicDetails settingSeismicDetails) {
        int i = -1;
        try {
            String sql = "delete from ypg_result_total_details " +
                    "where task_id='"+taskId+"' and sort='"+settingSeismicDetails.getFaultNum()+"'" +
                    "and magnitude='"+settingSeismicDetails.getEarthquakeLevel()+"' " +
                    "and angle='"+settingSeismicDetails.getAngle()+"'";

            i = jdbcTemplate.update(sql);
        }catch (Exception e){
            e.printStackTrace();
        }

        return i;
    }

    public List<PointVO> getPointVOByTaskIdNew(String taskId,String detailsId) {
        String sql="select c.longitude,c.latitude,c.macro_position,c.earthquake_level,c.fault_num " +
                "from seismic_influencefly_pbe a left join setting_seismic_details c on a.details_id =c.id " +
                " where a.task_id='"+taskId+"' and c.id='"+detailsId+"' and c.is_delete='0' " +
                "group by c.longitude,c.latitude,c.macro_position,c.earthquake_level,c.fault_num";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PointVO.class));
    }

    public List<SeismicInfluencefliedVO> findByPointVONew(String taskId,String detailsId,PointVO pointVO) {
        String sql="select a.details_id earthquakeId,a.influencefly_id influencefliedId,c.longitude,c.latitude," +
                "c.macro_position,c.fault_num,c.earthquake_level,c.angle,b.dz_intensity,b.long_x,b.short_y " +
                "from seismic_influencefly_pbe a left join setting_seismic_influenceflied b on a.influencefly_id=b.uuid" +
                " left join setting_seismic_details c on a.details_id =c.id " +
                " where a.task_id=? and c.id='"+detailsId+"' and c.is_delete='0' and c.longitude='"+pointVO.getLongitude()+"' and c.latitude='"
                +pointVO.getLatitude()+"' and c.earthquake_level='"+pointVO.getEarthquakeLevel()+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SeismicInfluencefliedVO.class),taskId);
    }

    @Override
    public void saveYpgResultNew(String taskId, String detailsId, List<SettingSeismicDetails> list) {
        SettingSeismicDetails settingSeismicDetails = settingSeismicRepository.getSettingSeismicDetailsById(detailsId);
        if(settingSeismicDetails!=null){
            delResultTotalByTaskIdNew(taskId, settingSeismicDetails);
            delResultDetailsEntityByTaskIdNew(taskId, settingSeismicDetails);
        }

        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);

        Map<String, ResultMainEntity> listMap = new HashMap<>();
        List<PointVO> pointVOByTaskId = getPointVOByTaskIdNew(taskId,detailsId);
        for (PointVO pointVO : pointVOByTaskId) {
            //List<SeismicInfluencefliedVO> list1 = findByPointVONew(taskId,detailsId,pointVO);
            List<SeismicInfluencefliedVO> list1 = settingSeismicInfluencefliedRepository.findByPointVO(taskId,pointVO);

            for (SeismicInfluencefliedVO s : list1) {
                ResultDetailsEntity entity = new ResultDetailsEntity();
                entity.setId(UUIDGenerator.getUUID());
                entity.setTaskId(taskId);
                entity.setSort(String.valueOf(s.getFaultNum()));
                entity.setLongitude(String.valueOf(s.getLongitude()));
                entity.setLatitude(String.valueOf(s.getLatitude()));
                entity.setMacroPosition(s.getMacroPosition());
                entity.setMagnitude(s.getEarthquakeLevel());
                entity.setAngle(String.valueOf(s.getAngle()));

                //根据地震影响场id去获取当前公里格网
                List<PbeEntityVO> pbe = pbeService.getPbe2ByInfluenceflieId(s.getInfluencefliedId());

                //获取导入地震影响场总面积（平方米）
                //String totalArea = influencefliedMapService.getSeismicInfluenceflyPbeArea(s.getInfluencefliedId());
                Integer totalArea = settingSeismicRepository.getArea(s.getInfluencefliedId(), taskId);
                BigDecimal disasterArea2 = BigDecimal.ZERO;
                if (!PlatformObjectUtils.isEmpty(totalArea)) {
//                    disasterArea2 = BigDecimal.valueOf(Double.parseDouble(totalArea)/1000000);//平方公里
                    disasterArea2 = BigDecimal.valueOf(totalArea);//平方公里
                }
                BigDecimal disasterNum2 = BigDecimal.ZERO;
                BigDecimal disasterGDP2 = BigDecimal.ZERO;
                for (PbeEntityVO pbeEntity : pbe) {
                    // disasterArea2 = disasterArea2.add(BigDecimal.valueOf(1));
                    disasterNum2 = disasterNum2.add(BigDecimal.valueOf(pbeEntity.getPopulation()));
                    disasterGDP2 = disasterGDP2.add(BigDecimal.valueOf(pbeEntity.getGdp()));
                }
                entity.setDisasterArea(disasterArea2.setScale(0, BigDecimal.ROUND_UP));
                disasterNum2=disasterNum2.divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_UP);
                entity.setDisasterNum(disasterNum2.toString());
                disasterGDP2=disasterGDP2.divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_UP);
                entity.setDisasterGdp(disasterGDP2.toString());
                entity.setDataSource("2");
                entity.setDelFlag(YNEnum.N.toString());
                entity.setCreateId(userId);
                entity.setCreateName(sUser.getUserName());
                entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                if(s.getEarthquakeId().equals(detailsId)){
                    resultTotalRepository.saveResultDetailsEntity(entity);
                }

                String key = taskId + "-" + pointVO.getFaultNum() + "-" + pointVO.getLongitude() + "-" + pointVO.getLatitude() + "-"
                        + pointVO.getMacroPosition() + "-" + pointVO.getEarthquakeLevel() + "-" + "2";
                ResultMainEntity resultMainEntity = null;
                if (listMap.containsKey(key)) {
                    resultMainEntity = listMap.get(key);
                    resultMainEntity.setDisasterArea(disasterArea2);
                    for (int i = 0; i < 2; i++) {
                        if (i == 0) {
                            String s1 = compareTo(disasterNum2.setScale(2, BigDecimal.ROUND_UP), resultMainEntity.getDisasterNum());
                            resultMainEntity.setDisasterNum(s1);
                        } else {
                            String s1 = compareTo(disasterGDP2.setScale(2, BigDecimal.ROUND_UP), resultMainEntity.getDisasterGdp());
                            resultMainEntity.setDisasterGdp(s1);
                        }
                    }
                } else {
                    resultMainEntity = new ResultMainEntity();
                    resultMainEntity.setCreateId(userId);
                    resultMainEntity.setCreateName(sUser.getUserName());
                    resultMainEntity.setDisasterArea(disasterArea2);
                    resultMainEntity.setDisasterNum(disasterNum2.setScale(2, BigDecimal.ROUND_UP).toString() + "-0");
                    resultMainEntity.setDisasterGdp(disasterGDP2.setScale(2, BigDecimal.ROUND_UP).toString() + "-0");
                    listMap.put(key, resultMainEntity);
                }
            }

        }
        saveResultMainEntity(listMap);
    }
    //zyt修改--导入影响场保存end

    private String compareTo(BigDecimal num1, String num2) {
        // num1:5 8 4
        // num2:10-0
        String[] split = num2.split("-");//5 10
        for (int i = 0; i < split.length; i++) {
            if (i == 0 && num1.compareTo(new BigDecimal(split[0])) < 0) {
                if (split.length == 2) {
                    if (new BigDecimal(split[0]).compareTo(new BigDecimal(split[1])) > 0) {
                        split[0]=num1.setScale(2, RoundingMode.HALF_UP).toString();
                        split[1]=new BigDecimal(split[0]).setScale(2, RoundingMode.HALF_UP).toString();
                    } else {
                        split[0]=num1.setScale(2, RoundingMode.HALF_UP).toString();
                        split[1]=new BigDecimal(split[1]).setScale(2, RoundingMode.HALF_UP).toString();
                    }
                } else {
                    split[0]=num1.setScale(2, RoundingMode.HALF_UP).toString();
                    split[1]=new BigDecimal(split[1]).setScale(2, RoundingMode.HALF_UP).toString();
                }
            } else if (i == 1 && num1.compareTo(new BigDecimal(split[1])) > 0) {
                split[0]=new BigDecimal(split[0]).setScale(2, RoundingMode.HALF_UP).toString();
                split[1]=num1.setScale(2, RoundingMode.HALF_UP).toString();
            }
        }
        num2 = split[0] + "-" + split[1];
        return num2;
    }

    private void saveResultMainEntity(Map<String, ResultMainEntity> listMap) {
        for (String key : listMap.keySet()) {
            ResultMainEntity resultMainEntity = listMap.get(key);
            String[] split = key.split("-");
            resultMainEntity.setId(UUIDGenerator.getUUID());
            resultMainEntity.setTaskId(split[0]);
            resultMainEntity.setSort(split[1]);
            resultMainEntity.setLongitude(split[2]);
            resultMainEntity.setLatitude(split[3]);
            resultMainEntity.setMacroPosition(split[4]);
            resultMainEntity.setMagnitude(new BigDecimal(split[5]));

            String[] split1 = resultMainEntity.getDisasterNum().split("-");
            String[] split2 = resultMainEntity.getDisasterGdp().split("-");
            if (split1[0].equals(split1[1]) || split1[1].equals("0")) {
                resultMainEntity.setDisasterNum(split1[0]);
            }
            if (split2[0].equals(split2[1]) || split2[1].equals("0")) {
                resultMainEntity.setDisasterGdp(split2[0]);
            }

            resultMainEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
            resultMainEntity.setDelFlag(YNEnum.N.toString());
            resultMainEntity.setDataSource("2");
            resultTotalRepository.saveResultTotal(resultMainEntity);
        }
    }

    @Override
    public void updateResultMainEntity(Map<String, ResultMainEntity> listMap, String type) {
        for (String key : listMap.keySet()) {
            ResultMainEntity resultMainEntity = listMap.get(key);
            String[] split = key.split("-");
            resultMainEntity.setTaskId(split[0]);
            resultMainEntity.setSort(split[1]);
            resultMainEntity.setMagnitude(new BigDecimal(split[2]));

            switch (type) {
                case "1":
                    String[] split1 = resultMainEntity.getDisasterNum().split("-");
                    String[] split2 = resultMainEntity.getDisasterGdp().split("-");
                    if (split1[0].equals(split1[1]) || split1[1].equals("0")) {
                        resultMainEntity.setDisasterNum(split1[0]);
                    }
                    if (split2[0].equals(split2[1]) || split2[1].equals("0")) {
                        resultMainEntity.setDisasterGdp(split2[0]);
                    }
                    break;
                case "5":
                    String[] split22 = resultMainEntity.getDeathTollBefore().split("-");
                    String[] split23 = resultMainEntity.getInjuredNumBefore().split("-");
                    if (split22[0].equals(split22[1]) || split22[1].equals("0")) {
                        resultMainEntity.setDeathTollBefore(split22[0]);
                    }
                    resultMainEntity.setDeathTollAfter(resultMainEntity.getDeathTollBefore());

                    if (split23[0].equals(split23[1]) || split23[1].equals("0")) {
                        resultMainEntity.setInjuredNumBefore(split23[0]);
                    }
                    resultMainEntity.setInjuredNumAfter(resultMainEntity.getInjuredNumBefore());
                    break;
                case "6":
                    String[] split24 = resultMainEntity.getDisasterLossBefore().split("-");
                    if (split24[0].equals(split24[1]) || split24[1].equals("0")) {
                        resultMainEntity.setDisasterLossBefore(split24[0]);
                    }
                    resultMainEntity.setDisasterLossAfter(resultMainEntity.getDisasterLossBefore());
                    break;
                case "4":
                    String[] split21 = resultMainEntity.getResettledNumBefore().split("-");
                    if (split21[0].equals(split21[1]) || split21[1].equals("0")) {
                        resultMainEntity.setResettledNumBefore(split21[0]);
                    }
                    resultMainEntity.setResettledNumAfter(resultMainEntity.getResettledNumBefore());
                    break;
                case "2":
                    String[] split3 = resultMainEntity.getRescueForceBefore().split("-");
                    if (split3[0].equals(split3[1]) || split3[1].equals("0")) {
                        resultMainEntity.setRescueForceBefore(split3[0]);
                    }
                    resultMainEntity.setRescueForceAfter(resultMainEntity.getRescueForceBefore());
                    String[] split4 = resultMainEntity.getRescueForceZyBefore().split("-");
                    if (split4[0].equals(split4[1]) || split4[1].equals("0")) {
                        resultMainEntity.setRescueForceZyBefore(split4[0]);
                    }
                    resultMainEntity.setRescueForceZyAfter(resultMainEntity.getRescueForceZyBefore());
                    String[] split5 = resultMainEntity.getRescueForceMjBefore().split("-");
                    if (split5[0].equals(split5[1]) || split5[1].equals("0")) {
                        resultMainEntity.setRescueForceMjBefore(split5[0]);
                    }
                    resultMainEntity.setRescueForceMjAfter(resultMainEntity.getRescueForceMjBefore());
                    String[] split6 = resultMainEntity.getRescueForceQtBefore().split("-");
                    if (split6[0].equals(split6[1]) || split6[1].equals("0")) {
                        resultMainEntity.setRescueForceQtBefore(split6[0]);
                    }
                    resultMainEntity.setRescueForceQtAfter(resultMainEntity.getRescueForceQtBefore());
                    String[] split7 = resultMainEntity.getMedicalMattersBefore().split("-");
                    if (split7[0].equals(split7[1]) || split7[1].equals("0")) {
                        resultMainEntity.setMedicalMattersBefore(split7[0]);
                    }
                    resultMainEntity.setMedicalMattersAfter(resultMainEntity.getMedicalMattersBefore());
                    String[] split8 = resultMainEntity.getMedicalNumBefore().split("-");
                    if (split8[0].equals(split8[1]) || split8[1].equals("0")) {
                        resultMainEntity.setMedicalNumBefore(split8[0]);
                    }
                    resultMainEntity.setMedicalNumAfter(resultMainEntity.getMedicalNumBefore());
                    String[] split9 = resultMainEntity.getNurseNumBefore().split("-");
                    if (split9[0].equals(split9[1]) || split9[1].equals("0")) {
                        resultMainEntity.setNurseNumBefore(split9[0]);
                    }
                    resultMainEntity.setNurseNumAfter(resultMainEntity.getNurseNumBefore());
                    String[] split10 = resultMainEntity.getPsychologistBefore().split("-");
                    if (split10[0].equals(split10[1]) || split10[1].equals("0")) {
                        resultMainEntity.setPsychologistBefore(split10[0]);
                    }
                    resultMainEntity.setPsychologistAfter(resultMainEntity.getPsychologistBefore());
                    break;
                case "3":
                    String[] split11 = resultMainEntity.getTentNumBefore().split("-");
                    if (split11[0].equals(split11[1]) || split11[1].equals("0")) {
                        resultMainEntity.setTentNumBefore(split11[0]);
                    }
                    resultMainEntity.setTentNumAfter(resultMainEntity.getTentNumBefore());
                    String[] split12 = resultMainEntity.getStretcherNumBefore().split("-");
                    if (split12[0].equals(split12[1]) || split12[1].equals("0")) {
                        resultMainEntity.setStretcherNumBefore(split12[0]);
                    }
                    resultMainEntity.setStretcherNumAfter(resultMainEntity.getStretcherNumBefore());
                    String[] split13 = resultMainEntity.getQuiltsNumBefore().split("-");
                    if (split13[0].equals(split13[1]) || split13[1].equals("0")) {
                        resultMainEntity.setQuiltsNumBefore(split13[0]);
                    }
                    resultMainEntity.setQuiltsNumAfter(resultMainEntity.getQuiltsNumBefore());
                    String[] split14 = resultMainEntity.getFoodsNumBefore().split("-");
                    if (split14[0].equals(split14[1]) || split14[1].equals("0")) {
                        resultMainEntity.setFoodsNumBefore(split14[0]);
                    }
                    resultMainEntity.setFoodsNumAfter(resultMainEntity.getFoodsNumBefore());
                    String[] split15 = resultMainEntity.getWaterNumBefore().split("-");
                    if (split15[0].equals(split15[1]) || split15[1].equals("0")) {
                        resultMainEntity.setWaterNumBefore(split15[0]);
                    }
                    resultMainEntity.setWaterNumAfter(resultMainEntity.getWaterNumBefore());
                    String[] split16 = resultMainEntity.getBathroomNumBefore().split("-");
                    if (split16[0].equals(split16[1]) || split16[1].equals("0")) {
                        resultMainEntity.setBathroomNumBefore(split16[0]);
                    }
                    resultMainEntity.setBathroomNumAfter(resultMainEntity.getBathroomNumBefore());
                    String[] split17 = resultMainEntity.getShowersNumBefore().split("-");
                    if (split17[0].equals(split17[1]) || split17[1].equals("0")) {
                        resultMainEntity.setShowersNumBefore(split17[0]);
                    }
                    resultMainEntity.setShowersNumAfter(resultMainEntity.getShowersNumBefore());
                    String[] split18 = resultMainEntity.getSickroomNumBefore().split("-");
                    if (split18[0].equals(split18[1]) || split18[1].equals("0")) {
                        resultMainEntity.setSickroomNumBefore(split18[0]);
                    }
                    resultMainEntity.setSickroomNumAfter(resultMainEntity.getSickroomNumBefore());
                    String[] split19 = resultMainEntity.getSickbedNumBefore().split("-");
                    if (split19[0].equals(split19[1]) || split19[1].equals("0")) {
                        resultMainEntity.setSickbedNumBefore(split19[0]);
                    }
                    resultMainEntity.setSickbedNumAfter(resultMainEntity.getSickbedNumBefore());
                    String[] split20 = resultMainEntity.getPlasmaNumBefore().split("-");
                    if (split20[0].equals(split20[1]) || split20[1].equals("0")) {
                        resultMainEntity.setPlasmaNumBefore(split20[0]);
                    }
                    resultMainEntity.setPlasmaNumAfter(resultMainEntity.getPlasmaNumBefore());
                    break;
            }

            resultMainEntity.setUpdateId(PlatformSessionContext.getUserID());
            resultMainEntity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
            resultMainEntity.setDataSource("2");
            resultTotalRepository.updateResultTotal(resultMainEntity);
        }
    }

    @Override
    public void updateResultDetailsEntity(ResultDetailsEntity entity) {
        resultTotalRepository.updateResultDetailsEntity(entity);
    }

    @Override
    public ResultDetailsEntity getResultDetailsEntity(String taskId, int faultNum, BigDecimal earthquakeLevel, int angle) {
        return resultTotalRepository.getResultDetailsEntity(taskId, faultNum, earthquakeLevel, angle);
    }

    @Override
    public List<ResultDetailsEntity> getResultDetailsByTaskId(String taskId, String dataSource) {
        return resultTotalRepository.getResultDetailsEntityByTaskId(taskId, dataSource);
    }


    @Override
    public Map<String,Object> getAll(String taskId,Integer curPage,Integer pageSize) {
        Map<String,Object> page = resultTotalRepository.getResultTotalByTaskId(taskId,curPage,pageSize);
        return page;
    }

    @Override
    public List<ResultMainEntity> allNoPage(String taskId) {
        List<ResultMainEntity> list = resultTotalRepository.getAllByTaskId(taskId);
        return list;
    }


    @Override
    public RestResponse getAllDetails(String taskId) {
        return RestResponse.succeed(resultTotalRepository.getResultDetailsEntityByTaskId(taskId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse updateAll(List<ResultMainEntity> list) {
        int count=0;
        for (ResultMainEntity entity: list){
            entity.setUpdateId(PlatformSessionUtils.getUserId());
            entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
            int i = resultTotalRepository.updateResultTotal(entity);
            count=count+i;
        }
        return ResponseCreator.createSuccessResponse(count, "修改成功！");
    }

    @Override
    public List<ResultMainEntity> exportExcel(String taskId) {
        return resultTotalRepository.exportExcel(taskId);
    }

    @Override
    public void saveRange(String range, String taskId) {
        resultTotalRepository.saveRange(range,taskId);
    }

    @Override
    public void deleteRange(String taskId) {
        resultTotalRepository.deleteRange(taskId);
    }

    @Override
    public String getRangeByTaskId(String taskId) {
        return resultTotalRepository.getRangeByTaskId(taskId);
    }

}
